Helpful Information
 
 
Category: ASCII letters
ASCII letters => HTML entities

Hi,

I have lots of text like: ã à á ô õ É ê ª º and so on... and I want that letters to ahve their html entitie so it can be valid with XHTML 1.0 Transitional.

The thing is that htmlentities() or htmlspecialchars() will do that but it will also make html code like <br /> tags with html entities and I don't want that....

How can I do it just for that kind of letters?

Thanks.

please anyone... I really need this working...

Here's a suggestion:

Apply the htmlentities or htmlspecialchars function so that the letters will be encoded properly. Then, use preg_replace to convert the &amp;gt; and &amp;lt; back to > and <.

--Rick

but if I do that, it won't work at 100% cuz stuff like some punctuation as ! and ' will also be encoded and do not show what's it supposed to show...

I fixed...

I had to convert &amp; to &

but how can I make those 3 convertions in one line of code using preg_replace?

I never understood how to use that...

I'm not sure you can perform different character replacements using a single call to preg_replace(). This is because there is just one parameter position (the second) for specifying the replacement character.

There are at least a couple of options. One is to simply use as many calls to preg_replace as the number of characters you need to replace.

Another is to build an associative array and iterate through it, calling preg_replace on each iteration:

$replace = array("&lt;" => "<", "&gt;" => ">", "&amp;amp;" => "&");

$string = htmlentities($string);
foreach ($replace as $key => $value) {
$string = preg_replace("/$key/", $value, $string);
}


An advantage of this second approach is that if you find it necessary to replace other characters, all you need to do is add these to the array, rather than add a whole other line of code for the preg_replace call.

BTW, when I tested this with a string of the various punctuation characters, the only ones affected by htmlentities were the ampersand and the double quote.

--Rick

ok, I think that will do... thanks a lot guys










privacy (GDPR)